'use strict';

import { HttpException, HttpStatus } from '@nestjs/common';

export function timestampToTime(timestamp: string | Date) {
  if (!timestamp) return '';
  const date = new Date(timestamp);
  const Y = date.getFullYear() + '-';
  const M =
    (date.getMonth() + 1 < 10
      ? '0' + (date.getMonth() + 1)
      : date.getMonth() + 1) + '-';
  const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  const h =
    (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  const m =
    (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
    ':';
  const s =
    date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds() + '';
  return Y + M + D + h + m + s || ''; //
}

export function isChinese(temp) {
  if (/.*[\u4e00-\u9fa5]+.*$/.test(temp)) {
    return true;
  }
  return false;
}
export function throwHttp(message: string) {
  throw new HttpException(
    {
      status: HttpStatus.BAD_REQUEST,
      message: message,
    },
    HttpStatus.BAD_REQUEST,
  );
}
export interface SocketType {
  msg: String;
  code: Number;
  success: Boolean;
}
export function socketError(message: string): SocketType {
  return {
    msg: message,
    code: 500, // 自定义code
    success: false,
  };
}
export function socketSuccess(message: string): SocketType {
  return {
    msg: message,
    code: 200, // 自定义code
    success: true,
  };
}
interface Pagination {
  pageSize: number;
  pageNum: number;
}
export function getPagination({
  pageSize = 10,
  pageNum = 1,
}: Pagination): Pagination {
  const size = Number(pageSize) ?? 10;
  if (pageNum !== 0) {
    pageNum -= 1;
  }
  return {
    pageSize: size,
    pageNum,
  };
}
/**
 * 校验密码
 * @param {string} value 密码
 * @param {Function} callback 回调
 * @returns
 */
export function checkPassword(value: string) {
  let level = '';
  // 校验是数字
  const regex1 = /^\d+$/;
  // 校验字母
  const regex2 = /^[A-Za-z]+$/;
  // 校验符号
  const regex3 =
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]+$/;
  if (regex1.test(value)) {
    level = '低';
  } else if (regex2.test(value)) {
    level = '低';
  } else if (regex3.test(value)) {
    level = '低';
  } else if (/^[A-Za-z\d]+$/.test(value)) {
    level = '中';
  } else if (
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、\d]+$/.test(
      value,
    )
  ) {
    level = '中';
  } else if (
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、A-Za-z]+$/.test(
      value,
    )
  ) {
    level = '中';
  } else if (
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、A-Za-z\d]+$/.test(
      value,
    )
  ) {
    level = '高';
  }
  return level;
}
